<template>
  <page-view :title="false">
    <a-card :bordered="false" v-show="!spuEditShow">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="12">
              <a-form-item label="id">
                <a-input v-model="queryParam.id" placeholder />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="标题">
                <a-input v-model="queryParam.title" placeholder />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="品牌名">
                <a-input v-model="queryParam.brandName" placeholder />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="是否在售">
                <a-select v-model="queryParam.saleable" placeholder="请选择" default-value="0">
                  <a-select-option value="true">是</a-select-option>
                  <a-select-option value="false">否</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="{ float: 'right', overflow: 'hidden' }"
              >
                <a-button type="primary" @click="loadTableData()">查询</a-button>
                <a-button
                  style="margin-left: 8px"
                  @click="() => queryParam = {pageNo: 1, pageSize: 10}"
                >重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <a-table
        ref="table"
        :columns="columns"
        :rowKey="record => record.id"
        :dataSource="spuTableData"
        :pagination="pagination"
        :scroll="{ x: 1800 }"
        @change="handleTableChange"
      >
        <span slot="saleable" slot-scope="text">{{ text | yesOrNoFilter }}</span>

        <span slot="date" slot-scope="text">{{ text | dayjs }}</span>

        <span slot="action" slot-scope="text, record">
          <a style="margin-right:10px;" href="javascript:;" @click="handleEdit(record)">编辑</a>
          <a v-if="record.saleable" href="javascript:;" @click="offline(record)">下线</a>
          <a v-else href="javascript:;" @click="online(record)">上线</a>
        </span>
      </a-table>
    </a-card>

    <spu-edit v-show="spuEditShow" ref="spuEdit" @ok="handleSaveOk" @cancel="handleCancel" />
  </page-view>
</template>
<script>
import { PageView } from '@/layouts'
import SpuEdit from './SpuEdit'
import { spuPage, spuSaleableUpdate } from '@/api/product'

export default {
  name: 'SpuList',
  components: {
    PageView,
    SpuEdit
  },
  data() {
    return {
      columns,
      pagination: {},
      spuTableData: [],
      queryParam: { pageNo: 1, pageSize: 10 },
      spuEditShow: false
    }
  },
  methods: {
    handleTableChange(pagination, filters, sorter) {
      console.log(pagination)
      const pager = { ...this.pagination }
      pager.current = pagination.current
      this.pagination = pager
      this.queryParam = {
        pageSize: pagination.pageSize,
        pageNo: pagination.current,
        sortField: sorter.field,
        sortOrder: sorter.order,
        ...filters
      }
      this.loadTableData()
    },
    handleEdit(record) {
      this.spuEditShow = true
      this.$refs.spuEdit.handleEdit(record)
    },
    // handler
    handleSaveOk() {
      console.log('接受子組件的通知')
      this.spuEditShow = false
      this.loadTableData()
    },
    handleCancel() {
      this.spuEditShow = false
    },
    loadTableData() {
      // var _this = this
      console.log(this.queryParam)
      spuPage(this.queryParam).then(res => {
        const result = res.data
        this.spuTableData = result.records
        this.pagination = {
          total: result.total,
          pageSize: result.size,
          current: result.current
        }
        console.log(this.pagination)
      })
    },
    online(record) {
      spuSaleableUpdate({
        id: record.id,
        saleable: true
      }).then(res => {
        if (res.status === 0) {
          this.queryParam.id = record.id
          this.loadTableData()
          this.$message.success('上线成功')
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    offline(record) {
      spuSaleableUpdate({
        id: record.id,
        saleable: false
      }).then(res => {
        if (res.status === 0) {
          this.queryParam.id = record.id
          this.loadTableData()
          this.$message.success('下线成功')
        } else {
          this.$message.error(res.msg)
        }
      })
    }
  },
  created() {
    this.loadTableData()
  },
  filters: {
    yesOrNoFilter(value) {
      const valueMap = {
        true: '是',
        false: '否'
      }
      return valueMap[value]
    }
  }
}

const columns = [
  {
    title: 'id',
    dataIndex: 'id',
    key: 'id',
    width: '12%'
  },
  {
    title: '标题',
    dataIndex: 'title',
    key: 'title',
    width: '12%'
  },
  {
    title: '类目',
    dataIndex: 'cname',
    key: 'cname'
  },

  {
    title: '是否上线',
    dataIndex: 'saleable',
    key: 'saleable',
    scopedSlots: { customRender: 'saleable' }
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    key: 'createTime',
    scopedSlots: { customRender: 'date' }
  },
  {
    title: '更新时间',
    dataIndex: 'updateTime',
    key: 'updateTime',
    scopedSlots: { customRender: 'date' }
  },
  {
    title: '创建者',
    dataIndex: 'creater',
    key: 'creater',
    width: '5%'
  },
  {
    title: '更新者',
    dataIndex: 'updater',
    key: 'updater',
    width: '5%'
  },
  {
    title: '操作',
    scopedSlots: { customRender: 'action' },
    width: '20%'
  }
]
</script>
